package com.zlx.widget.IntegerEditText;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_util.WattLengthFilter;
import com.zlx.widget.R;

/**
 * <li>说明:数值输入框（保留小数点后x位）
 * <li>创建人：刘欢
 * <li>创建日期：2022/10/20
 * <li>修改人:
 * <li>修改日期：
 * <li>修改内容：
 */
public class IntegerEditText extends LinearLayout {
    EditText etDescription;
    TextView tvTicketContentLength;
    TextView tvTicketContentLimitLenth;
    LinearLayout llContent;
    private TypedArray typedArray;
    private int textSize;
    private int textColor;
    //声明上下文对象
    private Context context;
    //正常显示的行数
    private int line_number = 1;
    //声明文本视图对象
    private TextView text_view;
    //最大行数
    private int maxline_number = 4;
    //限制的字符长度
    private int limit_number = 200;
    private String hintText;

    public IntegerEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        //从布局文件中获取展示内容
        View inflate =LayoutInflater.from(context).inflate(R.layout.text_limit_length, this, true);
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.ViewLimitLengthEditView);
        hintText=typedArray.getString(R.styleable.ViewLimitLengthEditView_hint);
        maxline_number = typedArray.getInteger(R.styleable.ViewLimitLengthEditView_maxLine, 3);
        limit_number = typedArray.getInteger(R.styleable.ViewLimitLengthEditView_limitNumber, 200);
        etDescription = inflate.findViewById(R.id.etDescription);
        tvTicketContentLength= inflate.findViewById(R.id.tvTicketContentLenth);
        tvTicketContentLimitLenth= inflate.findViewById(R.id.tvTicketContentLimitLenth);
        tvTicketContentLimitLenth.setText(limit_number+"");

        if(!TextUtils.isEmpty(hintText)){
            etDescription.setHint(hintText);
        }

        etDescription.setMaxLines(maxline_number);

        //报活内容输入长度监听
        etDescription.setFilters(new InputFilter[]{new WattLengthFilter(limit_number, new WattLengthFilter.LengthWatch() {
            @Override
            public void onLengthChange(int length) {
                tvTicketContentLength.setText(length+"");
            }

            @Override
            public void onReachMaxLength() {
//                ToastUtils.showShort(getString(1,200));
            }
        })});
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setHintText(String hintText) {
        this.hintText = hintText;
    }

    public void setLine_number(int line_number) {
        this.line_number = line_number;
    }

    public void setText_view(TextView text_view) {
        this.text_view = text_view;
    }

    public void setMaxline_number(int maxline_number) {
        this.maxline_number = maxline_number;
    }

    public void setLimit_number(int limit_number) {
        this.limit_number = limit_number;
    }
}
